let app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bgTextStyle: 'dark',
        scrollTop: '200rpx',
        bgColor: '#ff0000',
        bgColorTop: '#00ff00',
        bgColorBottom: '#0000ff',
        nbTitle: '标题',
        nbLoading: false,
        nbFrontColor: '#000000',
        nbBackgroundColor: '#ffffff',
        swiperList: [{
                index: 0,
                active: false,
            }, {
                index: 1,
                active: false,
            },
            {
                index: 2,
                active: false,
            }, {
                index: 3,
                active: false,
            },
            {
                index: 4,
                active: false,
            },
        ],
        startPoint: 0,
        intervelHander: null,
        timeoutHande: null,
        activeIndex: 1
    },
    onLoad(option) {
        // this.automove()
    },
    pausemove() {
        this.intervelHander && clearInterval(this.intervelHander)
        this.timeoutHander && clearTimeout(this.timeoutHander)
        // this.timeoutHande = setTimeout(() => {
        //     this.automove()
        // }, 1000)
    },
    automove() {
        this.intervelHander && clearInterval(this.intervelHander)
        this.intervelHander = setInterval(() => {
            this.moveLeftorRight(1)
        }, 3000)
    },
    bindstart(e) {
        // let activeIndex=parseInt(e.currentTarget.dataset.index)+1;
        // let swiperList=this.data.swiperList;
        // if(swiperList.length==activeIndex){
        //     activeIndex=0;
        // };
        // console.log((swiperList.length+1)+" "+activeIndex);
        // this.setData({activeIndex})
    },
    start: function (e) {
        this.data.startPoint = e.changedTouches[0].pageX;
    },
    end: function (e) {
        let isLeft = 0;
        let endPoint = e.changedTouches[0].pageX;
        isLeft = (endPoint - this.data.startPoint);
        this.pausemove()
        if (isLeft < 0) {
            this.moveLeftorRight(1);
        }
        if (isLeft > 0) {
            this.moveLeftorRight(0);
        }
    },
    moveLeftorRight: function (idx) {
        let swp = this.data.swiperList;
        let activeIndex = this.data.activeIndex;
        let max = swp.length;
        let self = this;
        for (let j = 0; j < max; j++) {
            swp[j].active = true;
        }
        if (idx === 1) {
            if (swp[0] && swp[1]) {
                swp[0].swpClass = 'imgleft1';
                swp[1].swpClass = 'imgleft2';
                if (swp[2]) {
                    swp[2].swpClass = 'imgleft3';
                };
                activeIndex++;
                if (activeIndex >= swp.length) {
                    activeIndex = (activeIndex % swp.length) == swp.length ? 0 : (activeIndex % swp.length);
                };
                this.setData({
                    swiperList: swp
                }, () => {
                    swp.push(swp.shift());
                    self.setData({
                        swiperList: swp,
                        activeIndex
                    })
                })
            }
        } else {
            if (swp[1]) {
                swp[max - 1].swpClass = 'imgright1'
                swp[0].swpClass = 'imgright2'
                if (swp[2]) {
                    swp[1].swpClass = 'imgright3'
                }
                activeIndex--;
                if(activeIndex<0){
                    activeIndex=swp.length+activeIndex
                };
                self.setData({
                    swiperList: swp
                }, () => {
                    swp.unshift(swp.pop());
                    self.setData({
                        swiperList: swp,
                        activeIndex
                    })
                })
            }
        }
    }
})